﻿#include "realtimedata.h"
#include <QDebug>

static const QString sim[] =
{
    "入射功率","反射功率"
    ,"高频采样","-140V","-18V","12V","18V"
    ,"24V","音频信号","中放电流","调制电流","推动电流","频率"
};

static const QString stateText[] =
{
    "遥控状态", "手控状态", "准备状态", "播出状态", "低功率状态", "联锁报警", "激励报警", "主电源报警",
    "低压报警", "调制报警", "攻放报警", "过流报警", "反射报警", "播出故障", "备用", "备用"
};

RealTimeData::RealTimeData(QString devCode,QWidget *parent) : MyDialog(parent)
{
    this->devCode=devCode;
    Equipment *equ = globalData->GetEquByCode(devCode);

    this->setModal(false);
    this->setTitle(equ->devname);
    this->resize(1300,600);
    this->initchart(equ->devname);
    this->initWidget();
    this->setUpConnections();
}

void RealTimeData::initWidget()
{
    labTime=new QLabel();
    labLaunch=new QLabel();
    labLaunch1=new QLabel();
    labLaunch2=new QLabel();
    labRef=new QLabel();
    labRef1=new QLabel();
    labRef2=new QLabel();
    labZhu=new QLabel();
    labZhu1=new QLabel();
    labZhu2=new QLabel();

    CMDbox=new QComboBox();

    CMDbox->addItem(QString("空闲查询"));
    CMDbox->addItem(QString("状态查询"));
    CMDbox->addItem(QString("模拟查询"));
    CMDbox->addItem(QString("状态+模拟"));

    tableWidget1=new QTableWidget();
    tableWidget1->setRowCount(14);
    tableWidget1->setColumnCount(4);
    tableWidget1->verticalHeader()->hide();
    tableWidget1->horizontalHeader()->hide();
    for(int i=0;i<4;i++){
        tableWidget1->setColumnWidth(i,150);
    }
    for(int i=0;i<16;i++)
    {
        tableWidget1->setRowHeight(i,50);
    }
    for(int i=0;i<13;i++)
    {
        tableWidget1->setItem(i,0,new QTableWidgetItem(sim[i]));
    }

    int row = 0;
    for(int i=0;i<16;i++)
    {
        QLabel *lab =new QLabel();
        labList.append(lab);

        if(stateText[i] == "备用"){
            continue;
        }

        QHBoxLayout *layout = new QHBoxLayout();
        layout->setMargin(0);
        layout->addStretch();
        layout->addWidget(lab);
        layout->addStretch();
        QWidget *widget = new QWidget();
        widget->setLayout(layout);

        tableWidget1->setItem(row,2, new QTableWidgetItem(stateText[i]));
        tableWidget1->setCellWidget(row, 3, widget);//再指定单元格加入小部件
        row++;
    }

    labTime->setText(QChar(0xe7e8) + QString(" ") + QDateTime::currentDateTime().toString("HH:mm:ss"));
    labTime->setStyleSheet("color:green;");

    labLaunch1->setStyleSheet(GREEN1);
    labLaunch2->setText(QString("入射:"));
    labLaunch2->setStyleSheet("color:green;");
    labLaunch->setStyleSheet("color:green;");
    labLaunch->setText(QString::number(0));

    labRef1->setStyleSheet(BLUE);
    labRef2->setText(QString("反射:"));
    labRef2->setStyleSheet("color:green;");
    labRef->setStyleSheet("color:green;");
    labRef->setText(QString::number(0));

    labZhu1->setStyleSheet(WHITE);
    labZhu2->setText(QString("高频:"));
    labZhu2->setStyleSheet("color:green;");
    labZhu->setText(QString::number(0));
    labZhu->setStyleSheet("color:green;");


    QHBoxLayout *timelayout = new QHBoxLayout();
    timelayout->setMargin(0);
    timelayout->setSpacing(10);
    timelayout->addWidget(labTime);
    timelayout->addStretch();
    timelayout->addWidget(labLaunch1);
    timelayout->addWidget(labLaunch2);
    timelayout->addWidget(labLaunch);
    timelayout->addStretch();
    timelayout->addWidget(labRef1);
    timelayout->addWidget(labRef2);
    timelayout->addWidget(labRef);
    timelayout->addStretch();
    timelayout->addWidget(labZhu1);
    timelayout->addWidget(labZhu2);
    timelayout->addWidget(labZhu);
    //timelayout->addWidget(CMDbox);

    chartView = new ChartView(chart);
    QVBoxLayout *chartlayout=new QVBoxLayout();
    chartlayout->setMargin(0);
    chartlayout->setSpacing(10);
    chartlayout->addLayout(timelayout);
    chartlayout->addWidget(chartView);


    QHBoxLayout *butLayout = new QHBoxLayout();
    butLayout->setMargin(0);
    butLayout->setSpacing(10);
    butLayout->addWidget(tableWidget1);

    QHBoxLayout *mainLayout = new QHBoxLayout();
    mainLayout->setContentsMargins(0,0,10,0);
    mainLayout->setSpacing(10);
    //mainLayout->addStretch();
    mainLayout->addLayout(chartlayout);
    mainLayout->addLayout(butLayout);

    this->addLayout(mainLayout);
}

void RealTimeData::setUpConnections()
{
    timer = new QTimer();
    connect(timer,SIGNAL(timeout()),this,SLOT(showData()));
    timer->start(100);
    connect(CMDbox,SIGNAL(currentIndexChanged(int)),this,SLOT(CMDChange(int)));
}

void RealTimeData::showData()
{
    State *state = globalData->GetStateByCode(devCode);
    Simulation * sim= globalData->GetSimByCode(devCode);
    if(!state || !sim){
        return;
    }

    tableWidget1->setItem(0, 1, MyGUI::GetTableWidgetItem(sim->launch));
    tableWidget1->setItem(1, 1, MyGUI::GetTableWidgetItem(sim->reflect));
    tableWidget1->setItem(2, 1, MyGUI::GetTableWidgetItem(sim->highFreq));
    tableWidget1->setItem(3, 1, MyGUI::GetTableWidgetItem(sim->FOFZ));
    tableWidget1->setItem(4, 1, MyGUI::GetTableWidgetItem(sim->FOE));
    tableWidget1->setItem(5, 1, MyGUI::GetTableWidgetItem(sim->OT));
    tableWidget1->setItem(6, 1, MyGUI::GetTableWidgetItem(sim->OE));
    tableWidget1->setItem(7, 1, MyGUI::GetTableWidgetItem(sim->TF));
    tableWidget1->setItem(8, 1, MyGUI::GetTableWidgetItem(sim->audio));
    tableWidget1->setItem(9, 1, MyGUI::GetTableWidgetItem(sim->putting));
    tableWidget1->setItem(10,1, MyGUI::GetTableWidgetItem(sim->modulate));
    tableWidget1->setItem(11,1, MyGUI::GetTableWidgetItem(sim->drive));
    tableWidget1->setItem(12,1, MyGUI::GetTableWidgetItem(sim->fre));

    QList<int> stateList = state->getDataList();
    for(int i=0;i<16;i++)
    {
        labList.at(i)->setStyleSheet(stateList.at(i) == 0 ? RED : GREEN);
    }

    labTime->setText(QChar(0xe7e8) + QString(" ") + QDateTime::currentDateTime().toString("HH:mm:ss"));
    labLaunch->setText(QString::number(sim->launch));
    labRef->setText(QString::number(sim->reflect));
    labZhu->setText(QString::number(sim->highFreq));
    axisX->setRange(QDateTime::currentDateTime().addSecs(-21), QDateTime::currentDateTime().addSecs(-1));

    series0->append(QDateTime::currentDateTime().toMSecsSinceEpoch(),sim->launch);
    //qDebug()<<"launch"<<sim->datetime.time().toString();
    series1->append(QDateTime::currentDateTime().toMSecsSinceEpoch(),sim->reflect);
    series2->append(QDateTime::currentDateTime().toMSecsSinceEpoch(),sim->highFreq);
    chart->update();
    //qDebug()<<sim->datetime.toString("yyyy MM dd hh mm ss")<<sim->launch;
}

void RealTimeData::CMDChange( int sr)
{
   // int str=CMDbox->currentIndex();
//    int str=sr;
//    if(str==0)
//    {
//        //MainWindow::cmd=CMD1;
//        globalData->CMDAssignment(id,CMD1);
//    }
//    else if(str==1)
//    {
//        //MainWindow::cmd=CMD2;
//        globalData->CMDAssignment(id,CMD2);
//    }
//    else if(str==2)
//    {
//        //MainWindow::cmd=CMD3;
//        globalData->CMDAssignment(id,CMD3);
//    }
//    else if(str==3)
//    {
//       // MainWindow::cmd=CMD4;
//        globalData->CMDAssignment(id,CMD4);
//    }
//    emit globalData->seriPort();
}


void RealTimeData::initchart(QString name)
{
    int max=100;
    Equipment *equ = globalData->GetEquByCode(devCode);

//    if(equ->type == 0){
//        max=16;
//    }
//    else if(equ->type == 1)
//    {
//        max=70;
//    }
    series0 = new QLineSeries();//在折线图中显示数据
    series0->setVisible(true);
    series0->setPen(QPen(Qt::green,4,Qt::SolidLine));//颜色 粗细 形式

    series1 = new QLineSeries();
    series1->setVisible(true);
    series1->setPen(QPen(Qt::blue,4,Qt::SolidLine));

    series2=new QLineSeries();
    series2->setVisible(true);
    series2->setPen(QPen(QColor(255,255,255,200),4,Qt::SolidLine));

    axisX= new QDateTimeAxis;//将日期和时间添加到图表的轴
    axisX->setRange(QDateTime::currentDateTime().addSecs(-41), QDateTime::currentDateTime().addSecs(-1));
    axisX->setFormat("HH:mm:ss");
    axisX->setTickCount(5);
    axisX->setLinePenColor(QColor(Qt::gray));//轴的颜色
    axisX->setGridLineColor(QColor(Qt::gray));//网格线颜色
    axisX->setLabelsColor(QColor(255,255,255,200));//轴标签颜色

    QValueAxis *axisY = new QValueAxis;//相图表类的轴添加值
    axisY->setRange(0, max);
    axisY->setLabelFormat("%d");//保存周标签为double类型
    axisY->setTickCount(8);//分为8格
    axisY->setLinePenColor(QColor(Qt::gray));
    axisY->setLineVisible(true);//轴线和记号是否可见
    axisY->setGridLineColor(QColor(Qt::gray));
    axisY->setLabelsColor(QColor(255,255,255,200));


    chart = new Chart();
    chart->setBackgroundVisible(true);//隐藏图表背景
    chart->setBackgroundBrush(Qt::black);
    //chart->setTheme(QChart::ChartThemeBlueCerulean);

    chart->addAxis(axisX, Qt::AlignBottom);//axisX放到底部
    chart->addAxis(axisY, Qt::AlignLeft);//axisY放到左侧

    chart->addSeries(series0);
    chart->addSeries(series1);
    chart->addSeries(series2);
    chart->setAxisX(axisX,series0);//将axisX添加到水平轴 并作为水平轴附着给series0
    chart->setAxisY(axisY,series0);
    chart->setAxisX(axisX,series1);
    chart->setAxisY(axisY,series1);
    chart->setAxisX(axisX,series2);
    chart->setAxisY(axisY,series2);
}
